package com.parking.stall.service.call;


import com.parking.common.core.model.Result;
import com.parking.stall.client.form.OrderTimeForm;
import com.parking.stall.client.form.SelectDistanceCertifiedInfoForm;
import com.parking.stall.client.dto.*;
import com.parking.stall.exception.StallException;
import com.parking.stall.exceptionCode.StallExceptionCode;
import com.parking.stall.inter.CertifiedClient;
import com.parking.stall.inter.OrderClient;
import com.parking.stall.inter.RentersClient;
import org.aspectj.weaver.ast.Var;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Set;

/**
 * OpenFeign服务模块调用类
 */
@Component
public  class ServeCallClass {
    /**
     * rentersClient：出租客模块调用对象
     *certifiedClient：物业模块调用对象
     * orderClient: 订单模块调用
     */
    @Autowired
    private RentersClient rentersClient;
    @Autowired
    private CertifiedClient certifiedClient;
    @Autowired
    private OrderClient orderClient;
    /**
     * rentersCall:出租客账号调用方法
     * @param rentersId 出租客账号id
     * @return  SelectRentersDTO：出租客账号对象
     * @throws Exception
     */
    public SelectRentersDTO rentersCall(Integer rentersId)throws Exception{
        Result<SelectRentersDTO> result = rentersClient.selectRenters(rentersId);
        if (result.getCode()==500) {
            throw new StallException(StallExceptionCode.SYSTEM_ERROR);//系统错误
        }
        if (result.getCode()!=200) {
            throw new StallException(result.getMessage(),result.getCode());
        }
        SelectRentersDTO dto = result.getData();
        return dto;
    }

    /**
     * rentersInfoCall:出租客信息调用方法
     * @param rentersId：出租客账号id
     * @return SelectRentersInfoDTO：出租客信息对象
     * @throws Exception
     */
    public SelectRentersInfoDTO rentersInfoCall(Integer rentersId)throws Exception{
        Result<SelectRentersInfoDTO> result = rentersClient.selectRentersInfo(rentersId);
        if (result.getCode()==500) {
            throw new StallException(StallExceptionCode.SYSTEM_ERROR);//系统错误
        }
        if (result.getCode()!=200) {
            throw new StallException(result.getMessage(),result.getCode());
        }
        SelectRentersInfoDTO dto = result.getData();
        return dto;
    }

    /**
     * rentersInfoCall:出租客信息调用方法
     * @param rentersInfoId：出租客信息id
     * @return selectRentersInfoByIdDTO：出租客信息对象
     * @throws Exception
     */
    public SelectRentersInfoByIdDTO rentersInfoCallById(Integer rentersInfoId)throws Exception{
        Result<SelectRentersInfoByIdDTO> result = rentersClient.rentersInfoCallById(rentersInfoId);
        if (result.getCode()==500) {
            throw new StallException(StallExceptionCode.SYSTEM_ERROR);//系统错误
        }
        if (result.getCode()!=200) {
            throw new StallException(result.getMessage(),result.getCode());
        }
        SelectRentersInfoByIdDTO dto = result.getData();
        return dto;
    }

    /**
     * certifiedServerCall:物业信息调用方法
     * @param certifiedInfoAccountId：物业账号id
     * @return SelectCertifiedInfoDTO：物业信息对象
     * @throws Exception
     */
    public SelectCertifiedInfoDTO certifiedServerCall(Integer certifiedInfoAccountId)throws Exception{
        Result<SelectCertifiedInfoDTO> result = certifiedClient.selectCertifiedInfo(certifiedInfoAccountId);
        if (result.getCode()==500) {
            throw new StallException(StallExceptionCode.SYSTEM_ERROR);//系统错误
        }
        if (result.getCode()!=200) {
            throw new StallException(result.getMessage(),result.getCode());
        }
        SelectCertifiedInfoDTO dto = result.getData();
        return dto;
    }

    /**
     * distanceCertifiedCall：查询距离范围内的所有物业信息调用方法
     * @param form 查询距离范围内的所有物业信息
     * @return list 符合距离内的所有物业信息集合
     */
    public Set<DistanceCertifiedInfoDto> distanceCertifiedCall(SelectDistanceCertifiedInfoForm form) {
        Result<Set<DistanceCertifiedInfoDto>> listResult = certifiedClient.distanceCertifiedCall(form);
        if (listResult.getCode()==500) {
            throw new StallException(StallExceptionCode.SYSTEM_ERROR);//系统错误
        }
        if (listResult.getCode()!=200) {
            throw new StallException(listResult.getMessage(),listResult.getCode());
        }
        Set<DistanceCertifiedInfoDto> dtos = listResult.getData();
        return dtos;
    }

    /**
     * certifiedAccountCall：查询账号信息（车位模块调用）
     * @param certifiedAccountId:物业账号id
     * @return SelectCertifiedAccountIdDTO：账号信息返回参数
     */
    public SelectCertifiedAccountIdDTO certifiedAccountCall(Integer certifiedAccountId) {
        Result<SelectCertifiedAccountIdDTO> result = certifiedClient.certifiedAccountCall(certifiedAccountId);
        if (result.getCode()==500) {
            throw new StallException(StallExceptionCode.SYSTEM_ERROR);//系统错误
        }
        if (result.getCode()!=200) {
            throw new StallException(result.getMessage(),result.getCode());
        }
        SelectCertifiedAccountIdDTO dto = result.getData();
        return dto;
    }

    /**
     * certifiedInfoServerCall:物业信息及账号状态（车位模块调用）
     * @param certifiedInfoId
     */
    public SelectCertifiedInfoAndStateDTO certifiedInfoServerCall(Integer certifiedInfoId) {
        Result<SelectCertifiedInfoAndStateDTO> result = certifiedClient.certifiedInfoAndStateServerCall(certifiedInfoId);
        if (result.getCode()==500) {
            throw new StallException(StallExceptionCode.SYSTEM_ERROR);//系统错误
        }
        if (result.getCode()!=200) {
            throw new StallException(result.getMessage(),result.getCode());
        }
        SelectCertifiedInfoAndStateDTO dto = result.getData();
        return dto;
    }

    public Set<Integer> orderStallCall(OrderTimeForm form) throws Exception{
        Result<Set<Integer>> result = orderClient.userSelectTimeQuantum(form);
        if (result.getCode()==500) {
            throw new StallException(StallExceptionCode.SYSTEM_ERROR);//系统错误
        }
        if (result.getCode()!=200) {
            throw new StallException(result.getMessage(),result.getCode());
        }
        Set<Integer> setStallInfoIdS = result.getData();
        return setStallInfoIdS;
    }

    /**
     * selectCertifiedInfoByIds:根据物业信息ids获取物业信息的set集合
     * @param certifiedInfoIds
     * @return
     */
    public Set<CertifiedInfoByIdsDTO> selectCertifiedInfoByIds(Set<Integer> certifiedInfoIds) {
        Result<Set<CertifiedInfoByIdsDTO>> result = certifiedClient.selectCertifiedInfoByIds(certifiedInfoIds);
        if (result.getCode()==500) {
            throw new StallException(StallExceptionCode.SYSTEM_ERROR);//系统错误
        }
        if (result.getCode()!=200) {
            throw new StallException(result.getMessage(),result.getCode());
        }
        Set<CertifiedInfoByIdsDTO> data = result.getData();
        return data;
    }
}
